package com.bigmodel.util;

import com.bigmodel.entity.QAEntity;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

public class ExcelUtil {

    private static AtomicLong atomicLong = new AtomicLong();

//    public static void createExcel(List<QAEntity> qaEntityList, String filePath) {
//        for (QAEntity qaEntity : qaEntityList) {
//            atomicLong.incrementAndGet();
//        }
//        System.out.println("write excel total = "+atomicLong.incrementAndGet());
//    }
    public static void createExcel(List<QAEntity> qaEntityList, String filePath) {


        Workbook wb = new XSSFWorkbook();
        /*创建表单*/
        Sheet sheet = wb.createSheet("模板列表");

        Row row = null;
        Cell cell = null;

        row = sheet.createRow(0); //第一行为头

        cell = row.createCell(0);
        cell.setCellValue("编号");
        cell = row.createCell(1);
        cell.setCellValue("类别");
        cell = row.createCell(2);
        cell.setCellValue("问题输入");
        cell = row.createCell(3);
        cell.setCellValue("问题输出");


        int rowIndex = 1;  //当前行索引
        //创建Rows
        for (QAEntity qaEntity : qaEntityList) {

            atomicLong.incrementAndGet();

            row = sheet.createRow(rowIndex++);

            cell = row.createCell(0);
            cell.setCellValue(qaEntity.getCode());
            cell = row.createCell(1);
            cell.setCellValue(qaEntity.getCategory());
            cell = row.createCell(2);
            cell.setCellValue(qaEntity.getQuestionContent());
            cell = row.createCell(3);
            cell.setCellValue(qaEntity.getAnswerContent());

        }
        /*创建rows和cells完毕*/
        sheet.autoSizeColumn(0);
        sheet.autoSizeColumn(1);

        try {

            File file = new File(filePath);
            // 获取文件所在目录
            File directory = file.getParentFile();
            // 检查目录是否存在
            if (!directory.exists()) {
                // 创建目录
                boolean created = directory.mkdirs();
                if (created) {
                    System.out.println("目录已创建");
                } else {
                    System.out.println("目录创建失败");
                }
            } else {
            }

            wb.write(new FileOutputStream(filePath));
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                wb.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        System.out.println("write excel file ="+filePath+" total = "+atomicLong.incrementAndGet());

    }


    public static void createExcelSql(List<QAEntity> qaEntityList, String filePath) {


        Workbook wb = new XSSFWorkbook();
        /*创建表单*/
        Sheet sheet = wb.createSheet("模板列表");

        Row row = null;
        Cell cell = null;

        row = sheet.createRow(0); //第一行为头

        cell = row.createCell(0);
        cell.setCellValue("问题");
        cell = row.createCell(1);
        cell.setCellValue("prompt");
        cell = row.createCell(2);
        cell.setCellValue("SQL1");
        cell = row.createCell(3);
        cell.setCellValue("SQL2");
        cell = row.createCell(4);
        cell.setCellValue("SQL3");


        int rowIndex = 1;  //当前行索引
        //创建Rows
        for (QAEntity qaEntity : qaEntityList) {

            atomicLong.incrementAndGet();

            row = sheet.createRow(rowIndex++);

            cell = row.createCell(0);
            cell.setCellValue(qaEntity.getQuestionContent());
            cell = row.createCell(1);
            cell.setCellValue(qaEntity.getPrompt());
            cell = row.createCell(2);
            cell.setCellValue(qaEntity.getSql1());
            cell = row.createCell(3);
            cell.setCellValue(qaEntity.getSql2());
            cell = row.createCell(4);
            cell.setCellValue(qaEntity.getSql3());

        }
        /*创建rows和cells完毕*/
        sheet.autoSizeColumn(0);
        sheet.autoSizeColumn(1);

        try {

            File file = new File(filePath);
            // 获取文件所在目录
            File directory = file.getParentFile();
            // 检查目录是否存在
            if (!directory.exists()) {
                // 创建目录
                boolean created = directory.mkdirs();
                if (created) {
                    System.out.println("目录已创建");
                } else {
                    System.out.println("目录创建失败");
                }
            } else {
            }

            wb.write(new FileOutputStream(filePath));
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                wb.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        System.out.println("write excel file ="+filePath+" total = "+atomicLong.incrementAndGet());

    }



}
